package m.common.action;

import io.netty.handler.codec.http.HttpHeaderNames;
import m.appserver.RequestUtil;
import m.appserver.ResponseUtil;
import m.common.dao.Dao;
import m.common.model.SessionModel;
import m.common.model.UserModel;
import m.common.service.Service;
import m.system.RuntimeData;
import m.system.cache.CacheUtil;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.StringUtil;
import m.system.util.SystemUtil;

import java.io.File;
import java.util.Map;


public abstract class Action {
	public final static String SESSION_NAME="session_name";
	public abstract String getSessionLogin();
	/**
	 * 获取当前操作员
	 * 
	 */
	public UserModel getSessionLogUser(){
		return null;
	}
	/**
	 * 获取service实例
	 *  <T>
	 *  clazz
	 * 
	 *  MException
	 */
	public <T extends Service> T getService(Class<? extends T> clazz) throws MException{
		return RuntimeData.getService(clazz);
	}
	/**
	 * 获取dao实例
	 *  <T>
	 *  clazz
	 * 
	 *  MException
	 */
	public <T extends Dao> T getDao(Class<? extends T> clazz) throws MException{
		return RuntimeData.getDao(clazz);
	}
	public Dao getDao() throws MException{
		return RuntimeData.getDao(Dao.class);
	}
	public Service getService() throws MException{
		return RuntimeData.getService(Service.class);
	}

	/** sessionLogin
	 * 设置 session model
	 */
	public <T extends SessionModel> void setSessionModel(String sessionLogin,T model) {
		String sn=getSessionCookie()+sessionLogin;
		CacheUtil.push(sn, model.getOid());
	}
	/** sessionLogin 默认为getSessionLogin()
	 * 设置 session model
	 */
	public <T extends SessionModel> void setSessionModel(T model) {
		setSessionModel(getSessionLogin(),model);
	}
	/** sessionLogin
	 * 获取 session model
	 */
	public <T extends SessionModel> T getSessionModel(String sessionLogin,Class<T> clazz) {
		String sn=getSessionCookie()+sessionLogin;
		String oid=(String) CacheUtil.get(sn);
		if(!StringUtil.isSpace(oid)) {
			return CacheUtil.get(clazz,oid);
		}
		return null;
	}
	/** sessionLogin 默认为getSessionLogin()
	 * 获取 session model
	 */
	public <T extends SessionModel> T getSessionModel(Class<T> clazz) {
		return getSessionModel(getSessionLogin(),clazz);
	}
	/** sessionLogin
	 * 清除 session model
	 */
	public void removeSessionModel(String sessionLogin) {
		String sn=getSessionCookie()+sessionLogin;
		CacheUtil.clear(sn);
	}
	/** sessionLogin 默认为getSessionLogin()
	 * 清除 session model
	 */
	public void removeSessionModel() {
		removeSessionModel(getSessionLogin());
	}
	/**
	 * 获取当前登录的session
	 * 
	 */
	public String getSessionCookie() {
		if(!StringUtil.isSpace(authorization)) {
			String[] arr=authorization.split(" ");
			if(arr.length>1&&!StringUtil.isSpace(arr[1])&&!arr[1].equals("null")&&!arr[1].equals("undefined")) {
				return arr[1];
			}
		}
		String cookie=StringUtil.noSpace(request.getHeader(HttpHeaderNames.COOKIE));
		if(!"".equals(cookie)){
			for(String cs : cookie.split(";")){
				if(cs.trim().contains(Action.SESSION_NAME) &&cs.indexOf("=")>0) {
					return cs.substring(cs.indexOf("=")+1);
				}
			}
		}
		SystemUtil.println("no_cookie");
		return "no_cookie_"+GenerateID.generatePrimaryKey();
	}
	
	
	private RequestUtil request;
	private ResponseUtil response;
	private String authorization;
	private Map<String,File> fileMap;
	public String getAuthorization() {
		return authorization;
	}
	public void setAuthorization(String authorization) {
		this.authorization = authorization;
	}
	/**
	 * 获取上传文件流的Map
	 * 
	 */
	public Map<String, File> getFileMap() {
		return fileMap;
	}
	public void setFileMap(Map<String, File> fileMap) {
		this.fileMap = fileMap;
	}

	public String getRequestBody() {
		return request.getRequestBody();
	}
	public String getIpAddress() { 
		return getIpAddress(request);
	}
	public static String getIpAddress(RequestUtil req) {
		return req.getIpAddress();
	}
	public RequestUtil getRequest() {
		return request;
	}
	public void setRequest(RequestUtil request) {
		this.request = request;
	}
	public ResponseUtil getResponse() {
		return response;
	}
	public void setResponse(ResponseUtil response) {
		this.response = response;
	}
}
